package com.jddup.program.comparator;

import com.jddup.common.pojo.EntityAndDbDifference;
import com.jddup.common.pojo.TableColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by zhangzhenbin on 17-12-11.
 */
public interface DataTypeCompatibility {
    Logger logger = LoggerFactory.getLogger(DataTypeCompatibility.class);

    void compare(EntityAndDbDifference difference, TableColumn entityColumn, TableColumn tableColumn);

    String getAlterSQLTemplate(String tableName, String columnName);

    default EntityAndDbDifference.ModifyDDL getAlterSQL(EntityAndDbDifference.ModifyDDL.LevelEnums levelEnums, String tableName, String columnName, String typeFullName,String message) {
        return new EntityAndDbDifference.ModifyDDL(levelEnums,
                Arrays.asList(getAlterSQLTemplate(tableName, columnName)
                        .replace("{columnType}", typeFullName)),message);
    }

    default void putIntoDifference(EntityAndDbDifference difference,  EntityAndDbDifference.ModifyDDL modifyDDL, String columnName) {
        EntityAndDbDifference.ModifyDDL.LevelEnums level = modifyDDL.getLevel();
        List<String> ddls = modifyDDL.getDdls();
        difference.getDiffMap().put(columnName,modifyDDL);
        String msg = modifyDDL.getMsg() +"\n"+ ddls.stream().reduce((s, s2) -> "["+s + "]["+ s2 +"]").get();
        if(level.equals(EntityAndDbDifference.ModifyDDL.LevelEnums.serious)){
            logger.error(msg);
        } else {
            logger.warn(msg);
        }
    }
}
